package com.c.ctgproject.wiget.dialog

import android.os.Bundle
import android.view.View
import android.widget.RelativeLayout
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseDialogFragment
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerHttpResult
import com.c.ctgproject.utils.showToast
import kotlinx.android.synthetic.main.item_file_send_more_formalitese_dialog.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import java.lang.StringBuilder

/**
 * 作者:LiBW
 * 创建日期: 2021/7/6
 * 描述:手续发放签名 Dialog
 */
class FileSendMoreFormalitiesDialog(private val carIds: List<String>,private val listener:FileSendMoreFormalitiesCallback) : BaseDialogFragment() {
    private val mViewModel by lazy { ViewModelProvider(this)[FileManagerMoreViewModel::class.java] }
    var selectType = ""
    override fun viewIsCreated(view: View, savedInstanceState: Bundle?) {
        mInitSignatureBTN.setOnClickListener {
            mSignatureView.initPaint()
        }
        mConfirmBTN.setOnClickListener {
            handlerConfirm()
        }
        mSelectGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.m1 -> {
                    selectType = "1"
                }
                R.id.m2 -> {
                    selectType = "2"
                }
                R.id.m3 -> {
                    selectType = "3"
                }
            }
        }
        mViewModel.error.observe(this,  {
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this,  {
            MyLog.d("手续发放回调:$it")
            requireActivity().handlerHttpResult(it){
                showToast("手续发放成功")
                listener.onDialogDismiss()
            }
        })
    }


    private fun handlerConfirm() {
        when{
            selectType.isEmpty()->showToast("请选择手续发放方式")
            else->{
                val path = mSignatureView.saveImage()
                if (path == "0") {
                    showToast("发生异常,请稍后再试")
                } else {
                    val files= ArrayList<MultipartBody.Part>()
                    //文件
                    val file= File(path)
                    val requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
                    val fileBody=MultipartBody.Part.createFormData("image", file.name, requestBody)
                    files.add(fileBody)
                    //字段
                    val giveMethod=MultipartBody.Part.createFormData("giveMethod", selectType)
                    files.add(giveMethod)
                    //列表
                    val sb=StringBuilder()
                    carIds.forEach {
                        sb.append("$it,")
                    }
                    MyLog.d("传递的列表1:${sb}")
                    val str=sb.toString().substring(0,sb.toString().length-1)
                    val list=MultipartBody.Part.createFormData("carIds", str)
                    MyLog.d("传递的列表2:${str}")
                    files.add(list)
                    MyLog.d("我的天地址是:$path")
                    mViewModel.batchGiveProcedure(files)
                }
            }
        }
    }

    override fun onStart() {
        super.onStart()
        dialog!!.window!!.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
    }

    override fun getLayout() = R.layout.item_file_send_more_formalitese_dialog
    interface FileSendMoreFormalitiesCallback{
        fun onDialogDismiss()
    }
}